import React from 'react';
import {notification} from 'antd';
import moment from 'moment';
import { Select, Checkbox, Radio ,Tree } from 'antd';
const { Option } = Select;
const {TreeNode} = Tree
//弹出通知提醒框
function openNotification(type, message,time) {
    notification.open({
        type: type,
        message: message,
        duration: time?time:2,
    });
};
//得到当前时间 2020-06-19 00：00：00
function  getNowDate () {
    const temDate = new Date();
    const year = temDate.getFullYear();
    const month = temDate.getMonth() + 1;
    const date = temDate.getDate();
    const dateString = year + '-' + month + '-' + date + ' ' + '00:00:00';
    return (dateString);
}
//得到当前时间 2020-06-19
function getDate (date)  {
    return moment(date).format("YYYY-MM-DD")
}
//得到当前时间 分秒
function getDateHHMM (date)  {
   return moment(date).format("HH:mm")
}
//得到当前时间 年月
function getDateYYYYMM (date)  {
    return moment(date).format("YYYY-MM")
 }

//转换时间 2020-06-19 00:00:00
function getDateHHMMYY_HHMMSS (date)  {
    return moment(date).format("YYYY-MM-DD HH:mm:ss")
 }
//字符串转为时间  2020-06-19 00:00:00
function getDateByString (String)  {
    return moment(String,"YYYY-MM-DD HH:mm:ss")
 }
  //日期格式 转字符串
function getStringByDate (date,format)  {
  return moment(date).format(format)
}

 //生成Select的option
 function getOptionList(data, name,valueField) {
    if (!data) {
      return [];
    }
    const options = []; 
    if(Object.prototype.toString.call(data[0])==='[object Object]'){
      data.map((item,index) => {
        let value={};
        if(valueField instanceof Array){
          valueField.map(v=>value[v]=item[v]);
        }
       
        options.push(
          <Option value={valueField instanceof Array?JSON.stringify(value):item[`${valueField}`]} key={index} title={item[`${name}`]}>
            {item[`${name}`]}
          </Option>
        );
      });
    }else if(Object.prototype.toString.call(data[0])==='[object String]' || Object.prototype.toString.call(data[0])==='[object Number]'){
      data.map((item,index) => {
        options.push(
          <Option value={item} key={index} title={item}>
            {item}
          </Option>
        );
      });
    }
    
    return options;
  }
  
  function getRadioList(data, name) {
    if (!data) {
      return [];
    }
    const radioOtion = []; // [<Option value="0" key="all_key">全部</Option>];
    data.map(item => {
      radioOtion.push(
        <Radio value={item.id} key={item.id} checked={item.checked}>
          {item[`${name}`]}
        </Radio>
      );
      return '';
    });

    return radioOtion;
  }
  function getCheckboxList(data) {
    if (!data) {
      return [];
    }
    const options = []; // [<Option value="0" key="all_key">全部</Option>];
    data.map(item => {
      options.push(
        <Checkbox value={item.id}>          
        {item.name}
        </Checkbox>
      );
      return '';
    });
    return options;
  }

  
 //生成TreeSelect的Tree Node
 function getTreeNodeList(data, name,valueField) {
  if (!data) {
    return [];
  }
  const options = []; 
  if(Object.prototype.toString.call(data[0])==='[object Object]'){
    data.map((item,index) => {
      let value={};
      if(valueField instanceof Array){
        valueField.map(v=>value[v]=item[v]);
      }
     
      options.push(
        <TreeNode value={valueField instanceof Array?JSON.stringify(value):item[`${valueField}`]} key={index} title={item[`${name}`]}>
          {item[`${name}`]}
        </TreeNode>
      );
    });
  }else if(Object.prototype.toString.call(data[0])==='[object String]' || Object.prototype.toString.call(data[0])==='[object Number]'){
    data.map((item,index) => {
      options.push(
        <TreeNode value={item} key={index} title={item}>
          {item}
        </TreeNode>
      );
    });
  }
  
  return options;
}
const pageStyle = { width: '100%', textAlign: 'right' }


export { openNotification,getNowDate,getDate,pageStyle,
  getDateHHMM,getDateHHMMYY_HHMMSS,getDateYYYYMM,getDateByString,getOptionList,
  getRadioList,getCheckboxList,getStringByDate,getTreeNodeList
};
